Skip to content

Check the password definition on every save of the users repository - #21

Merged
byjg merged 1 commit into
masterfrom
7.0
Oct 29, 2026
Merged

byjg merged 1 commit into
masterfrom
7.0

Conversation

@byjg

@byjg byjg commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Changes

  • New ByJG\Authenticate\Validator\PasswordDefinitionValidator, a MicroORM EntityValidatorInterface. A plain password outside the PasswordDefinition throws InvalidArgumentException; a stored hash (bcrypt or SHA-1) is not checked.
  • UsersService sets it on the users repository when it is given a PasswordDefinition. A user created with new UserModel(...) used to be saved without any check (only users created or loaded by the service had the definition); now every save is checked, through UsersService, UsersRepository or MicroORM's Repository.
  • A validator declared on the user model's #[TableAttribute(validator: ...)] still runs after it.

Projects using byjg/config get the definition through DI (it is injected into UsersService's constructor), so a model no longer needs to fetch it from the container.

Tests

PasswordUpgradeTest: a weak password is rejected and a valid one saved through the service, UsersRepository and Repository; the model's own validator still runs; a loaded user with an old SHA-1 hash weaker than the rules is saved unchanged. Removing the wiring or the chaining fails the matching tests. PHPUnit 186 tests and Psalm pass.

- New PasswordDefinitionValidator (a MicroORM EntityValidatorInterface): a plain password
  outside the definition throws InvalidArgumentException, a stored hash is not checked
- UsersService sets it on the users repository when it has a PasswordDefinition, so a user
  created with new and saved through UsersRepository or MicroORM's Repository is checked too
- The validator declared on the user model's TableAttribute still runs
@byjg
byjg merged commit 43b753d into master Oct 29, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant